using System;
using Gtk;

namespace utilities
{
	public class AlarmTimer : Gtk.TreeNode
	{
		private DateTime startTime;
		private TimeSpan duration, remain;
		private bool isAlarmSet = false;
		private int alarm_count = 0, sound_count = 0;
		private string comment = "";

		public DateTime StartTime { get { return startTime; } }
		public DateTime AlarmTime { get { return startTime+duration; } }
		public TimeSpan Duration { get { return duration; } }
		public TimeSpan Remain { get { return remain; } }

		[Gtk.TreeNodeValue (Column = 0)]
		public string StartTime_STR { get { return startTime.ToString(); } }
		[Gtk.TreeNodeValue (Column = 1)]
		public string AlarmTime_STR { get { return (startTime+duration).ToString(); } }
		[Gtk.TreeNodeValue (Column = 2)]
		public string Duration_STR { get { return duration.ToString(); } }
		[Gtk.TreeNodeValue (Column = 3)]
		public string Remain_STR { get { return remain.ToString(); } }
		[Gtk.TreeNodeValue (Column = 4)]
		public bool IsAlarmSet { get { return isAlarmSet; } }
		[Gtk.TreeNodeValue (Column = 5)]
		public int AlarmCount { get { return alarm_count; } }
		[Gtk.TreeNodeValue (Column = 6)]
		public string Comment { get { return comment; } }
		
		public AlarmTimer (string comment)
		{
			startTime = DateTime.Now;
			remain = duration = new TimeSpan(0, 0, 0, 0);
			this.comment = comment;
		}

		public AlarmTimer (TimeSpan d, string comment)
		{
			startTime = DateTime.Now;
			remain = duration = d;
			this.comment = comment;
		}

		public void decreaseRemain()
		{
			remain = remain.Subtract(new TimeSpan(0, 0, 0, 1));
		}
		
		public bool alarmReached()
		{
			return remain.CompareTo(new TimeSpan(0, 0, 0, 0)) < 0;
		}
		
		public void setNextAlarm()
		{
			alarm_count ++;
			startTime = DateTime.Now;
			remain = duration;
		}
		
		public void setAlarm(TimeSpan d)
		{
			isAlarmSet = true;
			startTime = DateTime.Now;
			remain = duration = d;
			alarm_count = 0;
		}

		public void noAlarm()
		{
			isAlarmSet = false;
		}

		public void alarmPlaySound()
		{
			sound_count = 0;
			GLib.Timeout.Add(1000, new GLib.TimeoutHandler(playSound));
		}
		
		public bool playSound() {
		 	System.Media.SystemSounds.Beep.Play();
		    System.Media.SystemSounds.Asterisk.Play();
		    System.Media.SystemSounds.Exclamation.Play();
		    System.Media.SystemSounds.Question.Play();
		    System.Media.SystemSounds.Hand.Play();				
			
			sound_count ++;
			return (sound_count <= 2);
		}
	}
}

